package com.chixing.controller;


import com.chixing.entity.Hotel;
import com.chixing.entity.VO.HotelSearchVO;
import com.chixing.service.IHotelService;
import com.chixing.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-09-30
 */
@CrossOrigin
@RestController
@RequestMapping("/hotel")
public class HotelController {

    @Autowired
    private IHotelService hotelService;

    @GetMapping("hotelName/{hotelName}")
    public Result getName(@PathVariable("hotelName") String hotelName){
        return hotelService.getName(hotelName);
    }

    @GetMapping("hotel/{id}")
    public Result getById(@PathVariable("id") Long id){
        return hotelService.getById(id);
    }

    @GetMapping("hotelId/{hotelId}")
    public Result getByHotelId(@PathVariable("hotelId") Integer hotelId){
        return hotelService.getByHotelId(hotelId);
    }
    /**
     * 根据地点查询酒店列表（不分页）
     */
    @GetMapping("/search")
    public Result searchHotelsByLocale(
            @RequestParam(value = "location", required = false, defaultValue = "") String location) {

        System.out.println("查询: " + location);
        return hotelService.getHotelsByLocation(location);
    }


    @PostMapping("/searchAll")
    public Result searchHotels(@RequestBody HotelSearchVO vo) {
        return hotelService.searchHotels(vo);
    }

    @GetMapping("hot")
    public Result getHotHotel() {
        List<Hotel> hotHotelList = hotelService.getHotHotel();
        if (hotHotelList!=null && hotHotelList.size()>0) {
            return Result.getSuccess(hotHotelList);
        }else {
            return Result.getFail("暂无热门酒店");
        }
    }
    //推荐引擎
    @GetMapping("/recommend")
    public Result recommendHotels(@RequestParam(required = false)Long userId){
        List<Hotel> resultList;
        if (userId!=null) {
            //登录状态->个性化推荐
            resultList = hotelService.getRecommendHotel(userId);
        }else {
            //未登录->热门酒店
            resultList = hotelService.getHotHotel();
        }
        if (resultList ==null || resultList.isEmpty()) {
            return Result.getFail("暂无推荐酒店");
        }
        return Result.getSuccess(resultList);
    }



    /**
     * ES进行关键词查询
     * @param keyword
     * @return
     */
    @GetMapping("/searchES")
    public ResponseEntity<List<Hotel>> searchByKeyword(@RequestParam String keyword) {
        List<Hotel> hotels = hotelService.getByKeywordAndPage(keyword); // 查询 ES
        return ResponseEntity.ok(hotels);
    }


    @PostMapping("/searchESA")
    public Result searchHotelESA(@RequestBody HotelSearchVO vo) {
        List<Hotel> hotels = hotelService.searchHotelES(vo);
        if (hotels.isEmpty()) {
            return Result.getFail("未查询到酒店信息");
        }
        return Result.getSuccess(hotels);
    }

}
